DailyRewardScene = {}
DailyRewardSceneFunc = {}
DailyRewardSceneFunc.__index = DailyRewardSceneFunc
setmetatable(DailyRewardScene, DailyRewardSceneFunc)

DailyRewardSceneFunc.onTest = function(self, data)
  print("DailyRewardSceneFunc.onTest", data)
end

DailyRewardSceneFunc.getNode = function()
  --NetworkRequest.gainGiftBagRandom("onTest", DailyRewardScene)
  local l = {}
  setmetatable(l, DailyRewardSceneFunc)
  l.node = CCBReader:load("DailyRewardScene.ccbi")
  DailyRewardScene.instance = l
  l:init()
  return l
end
DailyRewardSceneFunc.getScene = function()
  local sc = CCScene:create()
  sc:addChild(DailyRewardScene:getNode().node)
  return sc
end

DailyRewardSceneFunc.init = function(self)
  local loginNum = UserData.userinfo.host.triggerHash.clogin
  local pickNum = 0
  if loginNum==1 then
    pickNum = 1
  elseif loginNum>1 and loginNum<5 then
    pickNum = 2
  elseif loginNum>=5 then
    pickNum = 3
  end
  self.pickNum = pickNum
  TypeCast(self.node:getChildByTag(1010), "CCLabelTTF"):setString(string.format("点击纸牌，开始抽奖\n今天还可以抽奖%d次", pickNum))
  for i=1,9 do
    setObjectExtTable(self.node:getChildByTag(1001):getChildByTag(i), self)
  end
  --UserData.userinfo.host.cLoginRewards
end

DailyRewardSceneFunc.createItem = function(self, item)
  local layer = CCLayer:create()
  --local imgName = ""
  local sp = nil
  if item.type=="cash" then
    sp = CCSprite:create("cash2.png") 
  elseif item.type=="coin" then
    sp = CCSprite:create("coin2.png") 
  elseif item.type=="exploit" then
    sp = CCSprite:create("exploit.png") 
  elseif item.type=="soldiers" then
    sp = CCSprite:create("soldier2.png") 
  elseif item.type=="reputation" then
    sp = CCSprite:create("cash2") 
  elseif item.type=="item" then
    local tmp = Item:createWithId(item.id)
    local imgName = nameFromPath(tmp.itemConfig.asset)
    sp = CCSprite:create(imgName) 
  elseif item.type=="equip" then
    local tmp = Equip:createWithId(item.id)
    local imgName = nameFromPath(tmp.equipConfig.asset)
    sp = CCSprite:create(imgName) 
  elseif item.type=="decorate" then
    local tmp = Equip:createWithId(item.id)
    local imgName = nameFromPath(tmp.itemConfig.asset)
    sp = CCSprite:create(imgName) 
  end
  layer:addChild(sp)
  addLabel(layer, ccp(0, -20), tostring(item.num), "Helvetica", 28)
  return layer
end
DailyRewardSceneFunc.enableButtons = function(self, enable)
  for i=1,9 do
    local b = self.node:getChildByTag(1001):getChildByTag(i)
    TypeCast(b, "CCButton"):setEnabled(enable)
  end
end
DailyRewardSceneFunc.checkComplete = function(self)
  TypeCast(self.node:getChildByTag(1010), "CCLabelTTF"):setString(string.format("点击纸牌，开始抽奖\n今天还可以抽奖%d次", self.pickNum))
  if self.pickNum>0 then return end
  print("DailyRewardSceneFunc.checkComplete end", #self.items)
  self:enableButtons(false)
  for k,v in pairs(self.items) do
    local ma = CCMoveTo:create(0.5, ccp(20, 640))
    local ac = ActionUtil.createSeqAction({ma, CCCallFuncN:create(DailyRewardSceneFunc.onActionDone)})
    v:runAction(ac)
  end
end
DailyRewardSceneFunc.onActionDone = function(node)
  print("DailyRewardSceneFunc.onActionDone")
  if DailyRewardScene.instance==nil then return end
  MainScene.scene:removeChild(DailyRewardScene.instance.node, true)
  setTouchEnabledAll(MainScene.node, true)
  MainScene.setState()
  DailyRewardScene.instance = nil
end

DailyRewardSceneFunc.onGetRewardButton = function(self, node, event)
  print("DailyRewardSceneFunc.onGetRewardButton")
  NetworkRequest.gainGiftBagRandom("onGetRewardComplete", self)
  LoadScene:showLoadingScene()
  self:enableButtons(false)
  self.currentNode = node
  --local ba = CCBlink:create(0.2, 1)
  --local ac = ActionUtil.createSeqAction({ba, CCCallFuncN:create(onActionEnd)})
  --node:runAction(ac)
end
DailyRewardSceneFunc.onGetRewardComplete = function(self, data)
  self:enableButtons(true)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    NetworkRequest.syncData(res)
    local ba = CCBlink:create(0.2, 1)
    local ac = ActionUtil.createSeqAction({ba, CCCallFuncN:create(DailyRewardSceneFunc.onActionEnd)})
    self.currentNode:runAction(ac) 
  else
    showMessage(res.msg, ccc3(255,255,0)) 
  end
end

DailyRewardSceneFunc.onActionEnd = function(node)
  print("onActionEnd", node:getTag())
  local index = node:getTag()
  local item = DailyRewardScene:createItem(UserData.userinfo.host.cLoginRewards[index])
  item:setPosition(node:getPosition())
  if DailyRewardScene.instance.items==nil then DailyRewardScene.instance.items={} end
  DailyRewardScene.instance.items[#DailyRewardScene.instance.items+1] = item
  
  DailyRewardScene.instance.node:addChild(item)
  DailyRewardScene.instance.pickNum = DailyRewardScene.instance.pickNum-1
  DailyRewardScene.instance:checkComplete()
end

DailyRewardSceneFunc.onClose = function(self, node, event)
  MainScene.scene:removeChild(node:getParent(), true)
  setTouchEnabledAll(MainScene.node, true)
  MainScene.setState()
  DailyRewardScene.instance = nil
end
